(function() {
    Nokia.RadioBox = Nokia.OptionBox.extend({
        initialize: function(options) {
            var instance = this;

			var defaults = {
				wrapper: 'div',
				disableLabelSelection: true
            };

            instance.options = jQuery.extend(defaults, options);

            instance._super.apply(instance, [ instance.options]);

			instance.registerData("radiobox");

			instance._attachEvents();

			instance._loadDefaultStatus();

			instance.registerVibrationOn([ 'check' ]);

			instance.fireCallback('create');
        },

		_attachEvents: function() {
			var instance = this;
			var element = instance.element;
			var options = instance.options;
			var wrapper = instance.wrapper;

			wrapper.bind('mouseleave.radiobox', function(event) {
				wrapper.removeClass(instance.Styles.pushedClass);
			});

			var action = function(event) {
				instance.check(event);
			};

			wrapper.bind('click.radiobox', action);
			Nokia.util.bindEnter(wrapper, action);

			wrapper.bind('mouseenter.radiobox', function(event) {
				wrapper.addClass(instance.Styles.pushedClass);
			});
		},

		check: function(event) {
			var instance = this;

			instance.clearAll();

			instance._super(event);
		},

		clearAll: function() {
			var instance = this;
			var element = instance.element;
			var name = element.attr('name');
			var wrapperClass = '.' + instance.Styles.wrapperClass;
			var iconClass = '.' + instance.Styles.iconClass;

			jQuery(wrapperClass).find('[name=' + name + ']:radio').each(function() {
				var radio = jQuery(this);
				var radioInstance = radio.data('radiobox');
				var wrapper = jQuery(this).parent(wrapperClass);

				// bug on WRT 3.x when find('.classname') - using workaround instead
				var icon = jQuery(wrapper[0].firstChild);

				icon.removeClass(instance.Styles.prefixBaseClass + 'checked');
				icon.addClass(instance.Styles.prefixBaseClass + 'unchecked');

				radioInstance.fireCallback('clear');
			});
		},

		Styles: {
			iconClass: 'nokia-radiobox-icon',
			labelClass: 'nokia-radiobox-label',
			prefixBaseClass: 'nokia-radiobox-',
			pushedClass: 'nokia-radiobox-pushed',
			wrapperClass: 'nokia-radiobox'
		}
    });
})();